package backend

import (
	"app/models"
	"time"

	"github.com/GoAdminGroup/go-admin/modules/logger"
)

func CheckStatus() error {
	// 获取今天的日期
	date := time.Now().Format("2006-01-02")
	var results []*models.Orders
	err := models.Orm.Model(&models.Orders{}).Where("estimated_seedling_date", "=", date).Where("order_status", "<", 8).Find(results).Error
	if err != nil {
		return err
	}

	// 每笔订单都很重要,先不要批量修改,我们改一条记录一条
	for _, order := range results {
		order.OrderStatus = 8
		result := models.Orm.Save(order)
		if result.Error != nil {
			logger.Errorf("自动更新订单: %s,状态失败\n", order.Name)
		}
	}
	logger.Errorf("本批订单自动更新为待出苗成功")
	return nil
}
